#include "dialog.h"
#include "ui_dialog.h"
#include "noisemaker.h"

bool conn = false;

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    ui->NoFilter->setChecked(true);

/************Validators************/
    QIntValidator* intVal = new QIntValidator( -90, 90, ui->latLineEdit);
    ui->latLineEdit->setValidator(intVal);

    noiseMaker* loud = new noiseMaker( -90, 90, ui->latLineEdit);
    ui->latLineEdit->setValidator(loud);

    QIntValidator* intVal1 = new QIntValidator( -180, 180, ui->longLineEdit);
    ui->longLineEdit->setValidator(intVal1);

    noiseMaker* loud1 = new noiseMaker( -180, 180, ui->longLineEdit);
    ui->longLineEdit->setValidator(loud1);

    QIntValidator* intVal2 = new QIntValidator( 0, 20037, ui->rangeLineEdit);
    ui->rangeLineEdit->setValidator(intVal2);

    noiseMaker* loud2 = new noiseMaker( 0, 20037, ui->rangeLineEdit);
    ui->rangeLineEdit->setValidator(loud2);

    QIntValidator* intVal3 = new QIntValidator( 0, 65535, ui->portLineEdit);
    ui->portLineEdit->setValidator(intVal3);

    noiseMaker* loud3 = new noiseMaker( 0, 65535, ui->portLineEdit);
    ui->portLineEdit->setValidator(loud3);


/************Networking mumbo jumbo************/
    connect(ui->NoFilter, SIGNAL(pressed()), this, SLOT(clearPacketGroup()));
    connect(ui->RangeFilter, SIGNAL(pressed()), this, SLOT(clearPacketGroup()));
    connect(ui->PacketFilter, SIGNAL(pressed()), this, SLOT(clearPacketGroup()));
    connect(ui->Connect, SIGNAL(pressed()), this, SLOT(Connection()));

/************Clear Packets**************/
    connect(ui->ClearPackets, SIGNAL(pressed()), this, SLOT(clearPacks()));


}


void Dialog::clearPacketGroup()
{
    if(sender() == ui->NoFilter)
    {
        ui->NWS->setChecked(false);
        ui->Tele->setChecked(false);
        ui->Position->setChecked(false);
        ui->Weather->setChecked(false);
        ui->latLineEdit->clear();
        ui->rangeLineEdit->clear();
        ui->longLineEdit->clear();
    }

    if(sender() == ui->RangeFilter)
    {
        ui->NWS->setChecked(false);
        ui->Tele->setChecked(false);
        ui->Position->setChecked(false);
        ui->Weather->setChecked(false);
    }

    if(sender() == ui->PacketFilter)
    {
        ui->latLineEdit->clear();
        ui->rangeLineEdit->clear();
        ui->longLineEdit->clear();
    }


} // End of clear

void Dialog::Connection()
{
    if (conn == false)
    {
        socket = new QTcpSocket(this);
        connect(socket, SIGNAL(readyRead()), this, SLOT(updateLabel()));
        connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
        socket->connectToHost(ui->ServerlineEdit->text(), quint16(ui->portLineEdit->text().toInt()));

        QByteArray Authorization = "user scd0002 pass -1 vers testsoftware v1 ";

        if (ui->PacketFilter->isChecked())
        {
            Authorization.append("filter t/");

            if(ui->NWS->isChecked())
                Authorization.append("n");
            if(ui->Weather->isChecked())
                Authorization.append("w");
            if(ui->Tele->isChecked())
                Authorization.append("t");
            if(ui->Position->isChecked())
                Authorization.append("p");

        }

        else if (ui->RangeFilter->isChecked())
        {
            Authorization.append("filter r/");

            Authorization.append(ui->latLineEdit->text());
            Authorization.append("/");
            Authorization.append(ui->longLineEdit->text());
            Authorization.append("/");
            Authorization.append(ui->rangeLineEdit->text());

        }


        Authorization.append("\n");
        socket->write(Authorization);

        //QString connec = "# Application Status:  Connected";
        //ui->textBrowser->append(connec);

        conn = true;
        ui->ClearPackets->setDisabled(true);
        ui->serverGroup->setDisabled(true);
        ui->packetGroup->setDisabled(true);
        ui->rangeGroup->setDisabled(true);
        ui->Connect->setText("Disconnect");



        if(socket->waitForConnected())
        {
            QString connec = "# Application Status:  Connected";
            ui->textBrowser->append(connec);
        }
        else
        {
             QString error = "# Application Status: Error -- unable to connect.\n"
                             "# Please verify server name and port number\n."
                             "# Hit Disconnect to resume.";
             ui->textBrowser->append(error);
             QApplication::beep();
        } // end of invalid connection

    }

    else if (conn == true)
    {
        conn = false;

        QString dis = "# Application Status:  Disconnected";
        ui->textBrowser->append(dis);

        ui->ClearPackets->setEnabled(true);
        ui->serverGroup->setEnabled(true);

        if (ui->PacketFilter->isChecked())
            ui->packetGroup->setEnabled(true);
        if (ui->RangeFilter->isChecked())
            ui->rangeGroup->setEnabled(true);


        ui->Connect->setText("Connect");
        socket->disconnectFromHost();
        delete socket;
    }

} //end of connection



void Dialog::updateLabel()
{
    QByteArray message = socket->readLine();
    ui->textBrowser->append(message);

} // End updatelabel

void Dialog::clearPacks()
{
    if (sender() == ui->ClearPackets)
    {
        ui->textBrowser->clear();
    }
}


Dialog::~Dialog()
{
    delete ui;
}
